package com.tao.aszx.manager.config;


import com.tao.aszx.manager.interceptor.LoginAuthInterceptor;
import com.tao.aszx.manager.properties.UserAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Component
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    LoginAuthInterceptor loginAuthInterceptor;
    @Autowired
    UserAuthProperties userAuthProperties;

    /**
     *     配置允许跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 添加路径规则
                .allowedOrigins("http://localhost:3001","http://127.0.0.1:3001")  // 允许跨域的域名，*代表所有
//                .allowedOriginPatterns("http://localhost:3001/**")
                .allowedMethods("*") //允许的访问访问方法
                .allowCredentials(true)   //允许携带cookie
                .maxAge(3600)    //预检请求的缓存时间 单位是秒
                .allowedHeaders("*"); // 允许所有的请求头
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(loginAuthInterceptor)
                .excludePathPatterns(userAuthProperties.getNoAuthUrls())
                .addPathPatterns("/**");
    }
}
